<template>
  <el-container class="fullContainer">
    <el-header
      ><div class="title">
        <img src="../assets/heima.png" alt="" /><span style="margin-left: 10px"
          >电商后台管理系统</span
        >
      </div>
      <el-button class="logoutBtn" type="info" @click="logout()"
        >退出</el-button
      >
    </el-header>
    <el-container>
      <el-aside :width="isCollapse?'64px':'200px'">
        <div class="toggle-button" @click="toggleCollapse" >|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409fff"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menulist"
            :key="item.id"
          >
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
              <el-menu-item
                :index="'/'+childitem.path"
                v-for="childitem in item.children"
                :key="childitem.id"
                @click="saveNavState('/'+childitem.path)"
              >
                <i class="el-icon-menu"></i>
                <span>{{ childitem.authName }}</span>
              </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  //声明周期函数created,一上来就加载
  created(){
    this.getMenuList();
    this.activePath = window.sessionStorage.activePath;
  }, 
  data(){
      return{
        menulist: [],
        iconsObj:{
        '125':'iconfont icon-user',
        '103':'iconfont icon-tijikongjian', 
        '101':'iconfont icon-shangpin', 
        '102':'iconfont icon-danju', 
        '145':'iconfont icon-baobiao'},
        isCollapse:false,
        //被激活的链接地址
        activePath:''
      } ;
      
    },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList(){
      const {data : res} = await this.$http.get('menus')
      if (res.meta.status != 200){
        return this.$message.error(res.meta.msg);
      }
       this.menulist = res.data  
    },
    toggleCollapse(){
      this.isCollapse = !this.isCollapse
    },
    //保存链接激活状态
    saveNavState(activePath){
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  },
}
</script>
<style scoped>
.el-container {
}
.el-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: rgb(50, 60, 62);
  padding-left: 0%;
  color: aliceblue;
  font-size: 20px;
}
.el-aside {
  background-color: rgb(50, 50, 67);
}
.fullContainer {
  height: 100%;
}
.title {
  display: flex;
  align-items: center;
}
.el-main {
  background-color: rgb(233, 237, 240);
}
.iconfont{
  margin-right: 15px;
}
.el-menu{
  border-right: none;
}
.toggle-button{
  color: #fff;
  text-align: center;
  background-color: #475163;
  font-size: 10px;
  line-height: 24px;
}

</style>